package com.xjlsoft.weather.common.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * mac计算的公共函数
 * 
 * @author zhanghj
 */
public class DataUtil {
	private static String sg = "|";

	public static String DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public DataUtil() {
	}

	public static String getDateString(String date) {
		return getDateString(date, "000000");
	}

	public static String getDateString() {
		return getDateString(getCurrentDate(), getCurrentTime());
	}

	
	/***
	 * 格式化日期的方法
	 * 
	 * @param date
	 *            带格式化的日期
	 * @param pattern
	 *            格式化的表达式 比如 yyyy-MM-dd
	 * @return 格式化后的字符串
	 */
	public static String formatDate(Date date, String pattern) {
		// 建立日期FORMAT的实例
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		return df.format(date);
	}
	/**
	 * 将yyyyMMdd和HHmmss型时间字符串
	 * 
	 * @param day
	 * @return
	 */
	public static String getDateString(String date, String time) {
		String year = date.substring(0, 4);
		String month = date.substring(4, 6);
		String day = date.substring(6, 8);

		String hour = time.substring(0, 2);
		String min = time.substring(2, 4);
		String sec = time.substring(4, 6);

		return year + "-" + month + "-" + day + " " + hour + ":" + min + ":"
				+ sec;
	}

	/**
	 * 由yyyy-MM-dd HH:mm:ss型时间字符串 得到yyyyMMdd型时间字符串
	 * 
	 * @param day
	 * @return
	 */
	public static String chgDateString(String date) {
		// modify begin by maojs at 2006-5-16 ↓
		// if (null == date || "".equals(date)) return ""; //by befresh add at
		// 2004-09-16
		if (null == date || "".equals(date) || date.length() < 10)
			return ""; // by befresh add at 2004-09-16
		// end by maojs at 2006-5-16 ↑
		String year = date.substring(0, 4);
		String month = date.substring(5, 7);
		;
		String day = date.substring(8, 10);

		return year + month + day;
	}

	
	// 组成失败报文
	public static String compErrData(byte[] body, int dlen, String code,
			String reason) {
		System.out.println(new String(body));
		int len = body.length;
		int count = 0;
		int start = 0;

		String err = "";

		String datalen = "";

		for (start = len - 1;; start--) { // 从结束符前的"|"开始
			if (body[start] == 0x7c) {
				count++;
			}
			if (count == 4) {
				break;
			}
		}
		start += 1;
		byte[] nwb = new byte[start];
		for (int i = 0; i < start; i++) {
			nwb[i] = body[i];
		}
		// System.out.println(new String(nwb));
		reason = dealCharSet(reason);
		err = new String(nwb);
		err = err + code + sg + reason + sg + "0" + sg;

		byte bc[] = err.getBytes();
		byte bmac[] = getMac(bc, 8);

		String mac = "";
		mac = new String(bmac);
		err = err + mac + sg;

		int el = err.getBytes().length;
		datalen = String.valueOf(el);
		err = com(datalen, 4, "0") + err;
		// System.out.println(err);
		return err;
	}

	

	public static byte[] getMac(byte[] dt, int mlen) {
		int n, k, len, ilen;
		// original input String length
		len = dt.length;
		// 原始串和mac长度的余数
		k = len % mlen;
		//
		n = mlen - k;
		ilen = len + n;
		byte[] bt = new byte[ilen];
		for (int i = 0; i < len; i++) {
			dt[i] = (byte) (dt[i] > 0x00 ? dt[i] : dt[i] + 256);
			bt[i] = dt[i];
		}
		bt[len] = 127;

		byte bo[] = new byte[mlen];
		for (int i = 0; i < ilen;) {
			for (int j = 0; j < mlen; j++) {
				bo[j] ^= bt[i];
				i++;
			}
		}

		for (int i = 0; i < mlen; i++) {
			// 如果遇到\r \n

			if ((bo[i] >= 0x00 ? bo[i] : bo[i] + 256) >= 0x80) { // "\r"
				bo[i] ^= 0x80;
			}

			if (bo[i] == 0x0d) { // "\r"
				bo[i] = 0x4d;
			}
			if (bo[i] == 0x0a) { // "\n"
				bo[i] = 0x4a;
			}
			if (bo[i] == 0x3a) { // ":"
				bo[i] = 0x7a;
			}
			if (bo[i] == 0x7c) { // "|"
				bo[i] = 0x3c;
			}
			if (bo[i] == 0x00) { // "0"
				bo[i] = 0x40;
			}

		}
		return bo;
	}

	public static String getCurrentDate() {
		Date dnow = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		return formatter.format(dnow);
	}
	
	public static Timestamp getCurrenTimestamp() {
		Date dnow = new Date();
		Timestamp createTime= new Timestamp(dnow.getTime());
		return  createTime;
	}

	public static String getCurrentTime() {
		Date dnow = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
		return formatter.format(dnow);
	}

	// 判断交易类型
	public static boolean switchTrans(String transcode, String rcline) {
		boolean flag = true;
		int t = Integer.parseInt(transcode);
		int r = Integer.parseInt(rcline);
		if (t > r) {
			flag = false;
		}
		return flag;
	}

	// 将字节数组打印
	public static void printByte(byte[] b) {
		System.out.println(new String(b));
	}

	// 比较报文mac和计算mac是否相等
	public static boolean equalMac(byte[] yourmac, byte[] mymac) {
		boolean flag = false;
		// System.out.println("********** yourmac ****************");
		// int yi = yourmac.length;
		// for (int i = 0; i < yi; i++) {
		// System.out.print(yourmac[i] + " ");
		// }
		// System.out.println("");
		// System.out.println("*********** mymac ************");
		// int mi = mymac.length;
		// for (int i = 0; i < mi; i++) {
		// System.out.print(mymac[i] + " ");
		// }
		// System.out.println("");
		String ymac = new String(yourmac);
		String mmac = new String(mymac);
		if (ymac.equals(mmac)) {
			// System.out.println("$$$$$$$$$$$ MAC 校验正确$$$$$$$$$$$$");
			flag = true;
		} else {
			// System.out.println("?????????? MAC 校验错误 ?????????");
		}
		return flag;
	}

	public boolean decideDataStop(String str) {
		boolean result = false;
		int len = str.lastIndexOf("|0|");
		if (len != -1) {
			result = true;
		}
		return result;
	}


	public static String com(String s, int size, String st) {
		int len = s.length();
		int mlen = size;
		int w = mlen - len;
		String str = "";
		for (int i = 0; i < w; i++) {
			str += st;
		}
		str += s;
		return str;
	}

	public static String com(String s) {
		int len = s.length();
		int mlen = 8;
		int w = mlen - len;
		String st = "0";
		String str = "";
		for (int i = 0; i < w; i++) {
			str += st;
		}
		str += s;
		return str;
	}

	// 乱码处理
	public static String dealCharSet(String str) {
		try {
			str = new String(str.getBytes("ISO-8859-1"), "GBK");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return str;

	}

	/**
	 * 上传银行时，需要将money(112.55)转成(11255)
	 * 
	 * @param money
	 * @return
	 */
	public static String dealMoney(String money) {
		if (null == money || "".equals(money))
			return ""; // by befresh add at 2004-09-16
		// float f = Float.parseFloat(money);
		double d = Double.parseDouble(money);
		long c = Math.round(d * 100.0);
		c *= 10;
		return money = String.valueOf(c);
	}

	/**
	 * 记日志时，需将money(11255)转成(112.55)
	 * 
	 * @param money
	 * @return
	 */
	public static String backMoney(String money) {
		float f = Float.parseFloat(money);
		f = f / 1000;
		return money = String.valueOf(f);
	}

}
